//
//  MainTabBar.swift
//  新浪微博
//
//  Created by 李赛 on 15/5/10.
//  Copyright (c) 2015年 李赛. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
   
    private let buttonCount = 5
    
    override func layoutSubviews() {
        // 千万不要忘记调用父类的方法
        super.layoutSubviews()
        
        var index = 0
        
        var w = bounds.size.width / CGFloat(buttonCount)
        var h = bounds.size.height
        let frame = CGRectMake(0, 0, w, h)
        
        for view in subviews as! [UIView]{
            if (view is UIControl && !(view is UIButton)){
                // 修改按钮的位置
                view.frame = CGRectOffset(frame, CGFloat(index) * w, 0)
                
                index += (index == 1) ? 2 : 1
            }
        }
        composeButton.frame = frame
        composeButton.center = CGPointMake(center.x, h * 0.5)
        
    }
    
    /// 撰写按钮  懒加载方式
     lazy var composeButton:UIButton = {
        let button = UIButton()
        
        // 设置button的图片  和背景图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 懒加载中需要使用self
        self.addSubview(button)
        
        return button
    }()
    
}
